{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-08-08T23:26:00.313851Z",
     "start_time": "2025-08-08T23:25:59.993743Z"
    }
   },
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "s = pd.Series([1,2,np.nan,None,pd.NA])\n",
    "print(s)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0       1\n",
      "1       2\n",
      "2     NaN\n",
      "3    None\n",
      "4    <NA>\n",
      "dtype: object\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-08T08:26:41.491658Z",
     "start_time": "2025-08-08T08:26:41.484774Z"
    }
   },
   "cell_type": "code",
   "source": "print(s.isna())",
   "id": "f986dbb62fd4f93a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    False\n",
      "1    False\n",
      "2     True\n",
      "3     True\n",
      "4     True\n",
      "dtype: bool\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-08T08:27:12.074024Z",
     "start_time": "2025-08-08T08:27:12.068025Z"
    }
   },
   "cell_type": "code",
   "source": "print(s.isnull())",
   "id": "224eb45fa817dd90",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    False\n",
      "1    False\n",
      "2     True\n",
      "3     True\n",
      "4     True\n",
      "dtype: bool\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-08T08:30:23.708787Z",
     "start_time": "2025-08-08T08:30:23.702267Z"
    }
   },
   "cell_type": "code",
   "source": "df = pd.DataFrame([[1,pd.NA,2],[2,3,5],[None,4,6]])",
   "id": "bbcd74971706e2a7",
   "outputs": [],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-08T08:30:29.136032Z",
     "start_time": "2025-08-08T08:30:29.128022Z"
    }
   },
   "cell_type": "code",
   "source": "print(df)",
   "id": "e0b6569a0f158e9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     0     1  2\n",
      "0  1.0  <NA>  2\n",
      "1  2.0     3  5\n",
      "2  NaN     4  6\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-08T08:31:12.435006Z",
     "start_time": "2025-08-08T08:31:12.426977Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(df.isna())\n",
    "print(df.isnull())"
   ],
   "id": "a3bbd2d90becaf37",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       0      1      2\n",
      "0  False   True  False\n",
      "1  False  False  False\n",
      "2   True  False  False\n",
      "       0      1      2\n",
      "0  False   True  False\n",
      "1  False  False  False\n",
      "2   True  False  False\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-08T08:33:03.383990Z",
     "start_time": "2025-08-08T08:33:03.377816Z"
    }
   },
   "cell_type": "code",
   "source": "print(df.isna().sum())",
   "id": "bc2c029e041443c1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    1\n",
      "1    1\n",
      "2    0\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-08T08:39:10.219253Z",
     "start_time": "2025-08-08T08:39:10.209039Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(df)\n",
    "print(df.dropna())"
   ],
   "id": "5c21f52b2a1f1574",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     0     1  2\n",
      "0  1.0  <NA>  2\n",
      "1  2.0     3  5\n",
      "2  NaN     4  6\n",
      "     0  1  2\n",
      "1  2.0  3  5\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-08T08:40:51.584029Z",
     "start_time": "2025-08-08T08:40:51.575268Z"
    }
   },
   "cell_type": "code",
   "source": "print(df.dropna(how=\"all\"))",
   "id": "b92566fd35cf37b6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     0     1  2\n",
      "0  1.0  <NA>  2\n",
      "1  2.0     3  5\n",
      "2  NaN     4  6\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-08T23:28:18.876746Z",
     "start_time": "2025-08-08T23:28:18.873624Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data = {\n",
    "    \"id\": [1,2],\n",
    "    \"name\": [\"Alice\",\"Bob\"],\n",
    "    \"math\": [80,90],\n",
    "    \"english\": [90,80],\n",
    "    \"chinese\": [80,90]\n",
    "}\n",
    "df = pd.DataFrame(data)"
   ],
   "id": "74ea360352ba4e60",
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-08T23:28:24.939195Z",
     "start_time": "2025-08-08T23:28:24.935223Z"
    }
   },
   "cell_type": "code",
   "source": "print(df)",
   "id": "f46798a3bfa88573",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   id   name  math  english  chinese\n",
      "0   1  Alice    80       90       80\n",
      "1   2    Bob    90       80       90\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-09T01:47:43.847617Z",
     "start_time": "2025-08-09T01:47:43.839612Z"
    }
   },
   "cell_type": "code",
   "source": "print(pd.melt(df, id_vars=['id', 'name'], var_name='科目', value_name='成绩'))",
   "id": "a42a71d3c6da44d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   id   name       科目  成绩\n",
      "0   1  Alice     math  80\n",
      "1   2    Bob     math  90\n",
      "2   1  Alice  english  90\n",
      "3   2    Bob  english  80\n",
      "4   1  Alice  chinese  80\n",
      "5   2    Bob  chinese  90\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-09T02:05:02.035751Z",
     "start_time": "2025-08-09T02:05:02.031006Z"
    }
   },
   "cell_type": "code",
   "source": "df2 = pd.melt(df, id_vars=['id', 'name'], var_name='科目', value_name='成绩')",
   "id": "3e9b5536a4743ee3",
   "outputs": [],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-09T02:05:20.583088Z",
     "start_time": "2025-08-09T02:05:20.574580Z"
    }
   },
   "cell_type": "code",
   "source": "df2.sort_values('name', ascending=True)",
   "id": "9d8cf28b029d26d8",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   id   name       科目  成绩\n",
       "0   1  Alice     math  80\n",
       "2   1  Alice  english  90\n",
       "4   1  Alice  chinese  80\n",
       "1   2    Bob     math  90\n",
       "3   2    Bob  english  80\n",
       "5   2    Bob  chinese  90"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>name</th>\n",
       "      <th>科目</th>\n",
       "      <th>成绩</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>Alice</td>\n",
       "      <td>math</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>Alice</td>\n",
       "      <td>english</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>Alice</td>\n",
       "      <td>chinese</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>Bob</td>\n",
       "      <td>math</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>Bob</td>\n",
       "      <td>english</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2</td>\n",
       "      <td>Bob</td>\n",
       "      <td>chinese</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-09T02:05:26.513040Z",
     "start_time": "2025-08-09T02:05:26.509041Z"
    }
   },
   "cell_type": "code",
   "source": "print(df2)",
   "id": "5fc965b1b5200874",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   id   name       科目  成绩\n",
      "0   1  Alice     math  80\n",
      "1   2    Bob     math  90\n",
      "2   1  Alice  english  90\n",
      "3   2    Bob  english  80\n",
      "4   1  Alice  chinese  80\n",
      "5   2    Bob  chinese  90\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-09T02:05:39.116656Z",
     "start_time": "2025-08-09T02:05:39.112Z"
    }
   },
   "cell_type": "code",
   "source": "df2 = df2.sort_values('name', ascending=True)",
   "id": "2751cfa6fadad7e1",
   "outputs": [],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-09T02:05:43.000328Z",
     "start_time": "2025-08-09T02:05:42.996329Z"
    }
   },
   "cell_type": "code",
   "source": "print(df2)",
   "id": "e1623a8c7fceae38",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   id   name       科目  成绩\n",
      "0   1  Alice     math  80\n",
      "2   1  Alice  english  90\n",
      "4   1  Alice  chinese  80\n",
      "1   2    Bob     math  90\n",
      "3   2    Bob  english  80\n",
      "5   2    Bob  chinese  90\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-09T02:09:00.679190Z",
     "start_time": "2025-08-09T02:09:00.663845Z"
    }
   },
   "cell_type": "code",
   "source": "df2.pivot(index=['id', 'name'], columns='科目', values='成绩')",
   "id": "ce5318cdd48cd9f",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "科目        chinese  english  math\n",
       "id name                         \n",
       "1  Alice       80       90    80\n",
       "2  Bob         90       80    90"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>科目</th>\n",
       "      <th>chinese</th>\n",
       "      <th>english</th>\n",
       "      <th>math</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>id</th>\n",
       "      <th>name</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <th>Alice</th>\n",
       "      <td>80</td>\n",
       "      <td>90</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <th>Bob</th>\n",
       "      <td>90</td>\n",
       "      <td>80</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "94755f9c83470525"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
